<template>
  <div>
    <div><h1>自主出库</h1></div>
    <div>
      <div>
        <el-form :inline="true" :model="date_ch" :label-position="right" label-width="120px">
          <el-form-item label="车号:">
            <el-input v-model="date_ch.chehao" class="saptext" type="text"></el-input>
          </el-form-item>
          <el-form-item label="单号:">
            <el-input v-model="date.danhao" class="saptext" type="text"></el-input>
            <el-button type="primary" @click="selectsap" icon="el-icon-searchsousuo">搜索</el-button>
          </el-form-item>
        </el-form>
      </div>

      <div class="form-container">
        <el-form :inline="true" v-model="date" :label-position="right" label-width="120px">
          <el-form-item label="供货方:">
            <el-input class="jhlxtext" v-model="date.ghf" type="text"/>

          </el-form-item>
          <el-form-item label="购货方:">
            <el-select class="ymhttext" v-model="date.gouhf" placeholder="请选择">
              <el-option
                v-for="item in ymhthOptions"
                :key="item.id"
                :label="item.text"
                :value="item.text">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="客户名称:">
            <el-input class="jhlxtext" v-model="date.khmc" type="text"/>
          </el-form-item>
          <el-form-item label="货物名称:">
            <el-input class="jhrqtext" v-model="date.hwmc" type="text"/>
          </el-form-item>
          <el-form-item label="仓库编号:">
            <el-input class="gystext" v-model="date.ckbh" @change="selectckbh" type="text"/>
          </el-form-item>
          <el-form-item label="仓库名称:">
            <el-input class="gysmctext" v-model="date.ckmc" type="text"/>
          </el-form-item>
          <el-form-item label="原始单号:">
            <el-input class="sjxmtext" v-model="date.ysdh" type="text"/>
          </el-form-item>
          <el-form-item label="原始重量:">
            <el-input class="chtext" v-model="date.yszl" type="text"/>
          </el-form-item>
          <el-form-item label="规格:">
            <el-input class="chtext" v-model="date.guige" type="text"/>
          </el-form-item>
          <el-form-item label="运输类型:">
            <el-input class="chtext" v-model="date.yslx" type="text"/>
          </el-form-item>
          <el-form-item label="物流名称:">
            <el-input class="chtext" v-model="date.wlmc" type="text"/>
          </el-form-item>
          <el-form-item label="物料编号:">
            <el-input class="chtext" v-model="date.wlbh" type="text"/>
          </el-form-item>
          <el-form-item label="预发吨位:">
            <el-input class="chtext" v-model="date.yfdw" type="text"/>
          </el-form-item>
          <el-form-item label="内外销:">
            <el-input class="chtext" v-model="date.nwx" type="text"/>
          </el-form-item>
          <el-form-item label="包装物名:">
            <!--
                        <el-input class="chtext" v-model="date.bzwm" type="text"/>
            -->
            <el-select class="ymhttext" v-model="date.bzwm" placeholder="请选择">
              <el-option
                v-for="item in bzwmOptions"
                :key="item.id"
                :label="item.dm"
                :value="item.dm">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="手机号:">
            <el-input class="chtext" v-model="date.openid" type="text"/>
          </el-form-item>
        </el-form>
      </div>

      <div>
        <el-button @click="submitData">提交</el-button>
      </div>
    </div>
  </div>
</template>

<script>

import {
  countguozhong,
  selectshdz, selectWlccfwdm, selectZmmWb01HbyMBLNR, selectZmmWb01IbyMBLNR,
  tbGuozhongExists,
  tbGuozhonginsertzz
} from "@/api/system/Fdongbei/zd";


export default {
  data() {
    return {
      right: "",
      date_ch: {
        chehao: '',
      },
      date: {
        danhao: '',
        gouhf: '',
        khmc: '',
        ghf: '呼伦贝尔东北阜丰生物科技有限公司',
        shf: '',
        hwmc: '',
        ckbh: '',
        ckmc: '',
        ysdh: '',
        yszl: '',
        guige: '',
        yslx: '',
        wlmc: '',
        wlbh: '',
        yfdw: '',
        nwx: '内销',
        bzwm: '',
        dsf: '',
        openid: '',
      },
      ymhthOptions: [],
      bzwmOptions: [],
      guozhong: {
        chehao: '',
        cardId: '',
        czbh: '',
        wlmc: '',
        gysdm: '',
        gsmc: '',
        ckbh: '',
        ckmc: '',
        ddysdh: '',
        ddyszl: '',
        guige: '',
        yslx: '',
        wlmcc: '',
        gblx: '',
        czymc: '',
        gbzt: '',
        ymqysj: '',
        ddwlsl: '',
        wlbh: '',
        fkZdr: '',
        sdDwx: '',
        ddbh: '',
        sdBzwmc: '',
        kkRjbb: '',
        ycCcc: '',
        zzmtbs: '',
        songdf: '',
        fksj: '',
      },

    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      selectshdz().then(response => {
        this.ymhthOptions = response.data
      })
    },
    selectsap() {
      countguozhong({ddbh: this.date.danhao}).then(response => {
        if (response.data > 0) {
          this.$modal.alertWarning("单据已经使用");
          return;
        }
        try {
          selectZmmWb01HbyMBLNR({mblnr: this.date.danhao}).then(response => {
            if (response.data === undefined) {
              this.$modal.alertWarning("Zmm_Wb_01_H缺失")
            }
            this.date_ch = {
              chehao: response.data.chehao,
            };
          });
        } catch (error) {
          console.error('请求失败:', error);
        }

        try {
          selectZmmWb01IbyMBLNR({mblnr: this.date.danhao}).then(response => {
            if (response.data === undefined) {
              this.$modal.alertWarning("Zmm_Wb_01_I缺失")
            }
            const data = response.data;
            this.date = {
              gouhf: data.lgobe2,
              hwmc: data.maktx,
              ckbh: data.lgort,
              ckmc: data.lgobe1,
              wlbh: data.matnr,
              bzwm: data.baozh,
              yfdw: data.erfmg,
              nwx: '内销',
              danhao:this.date.danhao,
              ghf: '呼伦贝尔东北阜丰生物科技有限公司',
            };
            this.wl = {
              nxWx: this.date.nwx,
              wlbh: this.date.wlbh,
            }
            selectWlccfwdm(this.wl).then(response => {
              this.bzwmOptions = response.data
            })
          });
        } catch (error) {
          console.error('请求失败:', error);
        }

      })
    },
    submitData() {//提交
      if (this.date_ch.chehao === "") {
        this.qk()
        this.message("车号不能为空")
        return;
      }

      tbGuozhongExists({chehao: this.date_ch.chehao}).then(response => {
        if (response.msg === 'OK') {
          this.guozhong = {
            chehao: this.date_ch.chehao,
            cardId: '',
            czbh: 'DR',
            wlmc: this.date.hwmc,
            gysdm: this.date.gouhf,
            gsmc: this.date.ghf,
            ckbh: this.date.ckbh,
            ckmc: this.date.ckmc,
            ddysdh: this.date.ysdh,
            ddyszl: this.date.yszl,
            guige: this.date.guige,
            yslx: this.date.yslx,
            wlmcc: this.date.wlmc,
            gblx: 'ZZ_C',
            czymc: this.$store.state.user.name,
            gbzt: '待进厂',
            ymqysj: '',
            ddwlsl: '',
            wlbh: '',
            fkZdr: this.$store.state.user.name,
            sdDwx: '',
            ddbh: '',
            sdBzwmc: '',
            kkRjbb: '',
            ycCcc: '',
            zzmtbs: '',
            songdf: '',
            fksj: new Date().toISOString(),
          }

          tbGuozhonginsertzz(this.guozhong).then(response => {
            if (response.msg !== 0) {
              this.qk()

              this.$modal.alert("保存成功")

            } else {
              this.qk()
              this.$modal.alertWarning("保存失败")

            }
          })
        } else {
          this.qk()
          this.$modal.alertWarning(response.msg)
        }
      })
    }

  },
  qk() {
    this.date_ch = {
      chehao: '',
    },
      this.date = {
        danhao: '',
        gouhf: '',
        khmc: '',
        ghf: '呼伦贝尔东北阜丰生物科技有限公司',
        shf: '',
        hwmc: '',
        ckbh: '',
        ckmc: '',
        ysdh: '',
        yszl: '',
        guige: '',
        yslx: '',
        wlmc: '',
        wlbh: '',
        yfdw: '',
        nwx: '内销',
        bzwm: '',
        dsf: '',
        openid: '',
      },
      this.ymhthOptions = [],
      this.bzwmOptions = [],
      this.guozhong = {
        chehao: '',
        cardId: '',
        czbh: '',
        wlmc: '',
        gysdm: '',
        gsmc: '',
        ckbh: '',
        ckmc: '',
        ddysdh: '',
        ddyszl: '',
        guige: '',
        yslx: '',
        wlmcc: '',
        gblx: '',
        czymc: '',
        gbzt: '',
        ymqysj: '',
        ddwlsl: '',
        wlbh: '',
        fkZdr: '',
        sdDwx: '',
        ddbh: '',
        sdBzwmc: '',
        kkRjbb: '',
        ycCcc: '',
        zzmtbs: '',
        songdf: '',
        fksj: '',

      }

  }
};

</script>

<style scoped lang="scss">
.el-icon-searchsousuo {
  display: flex;
  align-items: center;
}

.el-input {
  width: auto;
  flex-grow: 3;
}

.el-button {
  margin-left: 10px; /* 调整按钮与输入框之间的间距 */
  flex-shrink: 0; /* 防止按钮在宽度不足时被压缩 */
}

.input-with-select {
  background-color: #fff;
}
</style>
